package com.lin.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FilterAndListenerDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 向页面写出中文字符, 测试乱码
        resp.getWriter().write("这是一个中文字符, 不设置编码会乱码");
        // !!!! 这里用强转会报错, Integer 不能强转为 String
        String onlineCount = req.getServletContext().getAttribute("onlineCount") + "";
        System.out.println("onlineCount: " + onlineCount);
        resp.getWriter().write("在线人数: " + onlineCount);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
